package com.xwm93up.basic.lambdatips.comparator;

import com.xwm93up.basic.lambdatips.User;
import com.xwm93up.basic.lambdatips.UserBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class OrderlyList {
    public static void main(String[] args) {
        List<UserBean> userBeanList = new ArrayList<>();
        UserBean bob1 = new UserBean(1,"bob", 17, "2005-11-02");
        UserBean kangkang = new UserBean(5,"kangkang", 21, "2001-12-02");
        UserBean alice = new UserBean(4,"alice", 20, "2002-11-02");
        UserBean jane = new UserBean(3,"jane", 19, "2003-11-02");
        UserBean jack = new UserBean(2,"jack", 18, "2004-11-02");

        userBeanList.add(bob1);
        userBeanList.add(kangkang);
        userBeanList.add(alice);
        userBeanList.add(jane);
        userBeanList.add(jack);

        OrderlyList orderlyList = new OrderlyList();
        // orderlyList.sortByComparator(userBeanList);//根据age从小到大排序
        //orderlyList.sortByStream(userBeanList);// Stream排序


        List<User> userList = new ArrayList<>();
        User bob2 = new User(1,"bob", 17, "2005-11-02");
        User kangkang2 = new User(5,"kangkang", 21, "2001-12-02");
        User alice2 = new User(4,"alice", 20, "2002-11-02");
        User jane2 = new User(3,"jane", 19, "2003-11-02");
        User jack2 = new User(2,"jack", 18, "2004-11-02");
        userList.add(bob2);
        userList.add(kangkang2);
        userList.add(alice2);
        userList.add(jane2);
        userList.add(jack2);
        orderlyList.sortByComparable(userList);

    }

    private void sortByComparable(List<User> userList) {
        //排序前
        System.out.println("排序前");
        userList.forEach(e -> System.out.println(e.toString()));
        Collections.sort(userList);
        System.out.println("排序后");
        userList.forEach(e -> System.out.println(e.toString()));
    }

    private void sortByStream(List<UserBean> userBeanList) {
        //排序前
        System.out.println("排序前");
        userBeanList.forEach(e -> System.out.println(e.toString()));
        userBeanList = userBeanList.stream().sorted(Comparator.comparing(UserBean::getAge)).collect(Collectors.toList());
        System.out.println("排序后");
        userBeanList.forEach(e -> System.out.println(e.toString()));
    }


    public void sortByComparator(List<UserBean> userList) {
        //排序前
        System.out.println("排序前");
        userList.forEach(e -> System.out.println(e.toString()));
        Collections.sort(userList, new Comparator<UserBean>() {
            @Override
            public int compare(UserBean o1, UserBean o2) {
                // return o1.getAge().compareTo(o2.getAge());
                if (o1.getAge() > o2.getAge()) {
                    return 1;
                } else if (o1.getAge() < o2.getAge()) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        System.out.println("排序后");
        userList.forEach(e -> System.out.println(e.toString()));
    }

}
